<?php
/**
 * The model file of blog module of ZenTaoPHP.
 *
 * The author disclaims copyright to this source code.  In place of
 * a legal notice, here is a blessing:
 * 
 *  May you do good and not evil.
 *  May you find forgiveness for yourself and forgive others.
 *  May you share freely, never taking more than you give.
 */
?>
<?php
class indexModel extends model
{
	
	private $session_word='yzcode';
	
    public function check_login($username,$pwd){
    	$pwd=md5($pwd);
        $data=$this->dao->select('*')->from('user_info')->where("username='$username' and pwd='$pwd'")->fetch();
        return $data;
    }  
    
   function check_word($word)
   {
        $recorded = isset($_SESSION[$this->session_word]) ? base64_decode($_SESSION[$this->session_word]) : '';
        $given    = $this->encrypts_word(strtoupper($word));
        return (preg_match("/$given/", $recorded));
    }
    
    function encrypts_word($word)
    {
        return substr(md5($word), 1, 10);
    }
    /**
     * 得到省级排序，从大到小,限制前10名
     */
    public function get_sheng_sort(){
    	//$sql="select count(a.sites_id) as num, b.name from sites_info as a left join area_info as b on a.area_id=b.id  group by a.area_id order by num desc  limit 10";
    	$result=$this->dao->select("count(a.sites_id) as num,b.name")
    			  ->from('sites_info')->alias('a')
    			  ->leftJoin('area_info')->alias('b')
    			  ->on('a.area_id=b.id')
    			  ->groupBy('a.area_id')
    			  ->orderBy('num desc')
    			  ->limit('10')->fetchAll();
    	return $result;
    }
    
	/**
     * 得到式级排序，全部市级
     */
    public function get_shi_sort($shengtitle){
    	//$sql="select count(a.sites_id) as num, b.name from sites_info as a left join area_info as b on a.area_id=b.id  group by a.area_id order by num desc  limit 10";
    	$result1=$this->dao->select('id')->from('area_info')->where("name='$shengtitle'")->fetch();
    	$id=$result1->id;
    	$result=array();
    	if(!empty($id)){
    		$result2=$this->dao->select('id,name')->from('area_info')
    						   ->where("reid=$id")/*->limit('15')*/->fetchAll();
    		$sortnum=array();
    		foreach($result2 as $key=>&$val){
    			$num_result=$this->dao->select('count(sites_id) as num')
    								  ->from('sites_info')
    								  ->where('area_id2='.$val->id)->fetch();
    			$val->num=$num_result->num;
    			$sortnum[]=$val->num;
    		}
    		array_multisort($sortnum, SORT_DESC, $result2);
    		$result=$result2;
    	}
    	return $result;
    }
  
	public function get_sites_info($where_to=null)
    {
    	$where="1=1";
		if(trim($where_to->key_word)!=''){
    		$where.=" and (a.sites_name like '%".$where_to->key_word."%' or a.main_domain like '%".$where_to->key_word."%')";
    	}
        if($where_to->type_id!='all'){
    		$where.=" and a.type_id=".$where_to->type_id;
    	}
    	if($where_to->area_id!='all'){
    		$where.=" and a.area_id=".$where_to->area_id;
    	}
   		if($where_to->unit_id!='all'){
    		$where.=" and a.unit_id=".$where_to->unit_id;
    	}
    	if($where_to->ipv6_status!='all'){
    		$where.=" and a.ipv6_status=".$where_to->ipv6_status;
    	}
    	$where.=" and a.status=1";
        return $this->dao->select('a.*,b.type_name,c.name as area_name,d.unit_name')->from('sites_info as a')
        			 ->leftJoin('type_info as b')->on('a.type_id=b.type_id')
        			 ->leftJoin('area_info as c')->on('a.area_id=c.id')
        			 ->leftJoin('unit_info as d')->on('a.unit_id=d.unit_id')
        		     ->where($where)->orderBy('a.sites_id asc') ->fetchAll();
    }
	public function get_info($where_to=null)
    {
    	$where="1=1 and status = 1";
		if(trim($where_to->key_words)!=''){
    		$where.=" and (sites_name like '%".$where_to->key_words."%' or main_domain like '%".$where_to->key_words."%')";
    	}
        return $this->dao->select('*')->from('sites_info')
        		     ->where($where)->orderBy('sites_id asc') ->fetchAll();
    }
}
